package prj3.gui;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;

import javax.imageio.ImageIO;

import prj3.SphericalCell;
import prj3.SphericalCoordinate;

/**
 *
 * @author  cwhalen
 */
public class EarthView extends javax.swing.JPanel {
    
    private BufferedImage map;
    
    private LinkedList cellGrid = new LinkedList();;
    
    private int gridSpacing = Constants.DEFAULT_GRID_SPACING;
    
    private final int offset = 20;
    private float colorOpacity = 0.5f;
    
    private double sun_Long = 0;
    private double sun_Latt = 0;
    
    /** Creates new form EarthView */
    public EarthView() {
        initComponents();
        this.map = loadImage("map.png");
    }
    
    public void setColorOpacity(float c)
    {
        this.colorOpacity = c;
    }
    
    private BufferedImage loadImage(String path) {
        BufferedImage image = null;
        URL imgURL = EarthView.class.getResource(path);
        try {
        	image = ImageIO.read(imgURL);
        } catch(IOException e) {
            System.out.println("IO error: " + e.getMessage());
        }
        return image;
    }
    
    
    public void updateView(LinkedList grid, double sun_Latt, double sun_Long) {
        this.cellGrid = (LinkedList)grid.clone();
    	this.sun_Latt = sun_Latt;
    	this.sun_Long = sun_Long;
    	this.repaint();
    }
    
    public void setGridSpacing(int sp) {
    	this.gridSpacing = sp;
    }
    
    private void drawSunPosition(Graphics2D g2) {
        g2.setStroke(new BasicStroke(3.0f));
        g2.setColor(new Color(200,200,125));

        if(Math.toDegrees(sun_Long)<=180)
        {
        	int x = (int)(Math.toDegrees(sun_Long)*2)+offset+360;
        	g2.drawLine(x,offset,x,this.getHeight());
        } else {
        	int x = (int)((Math.toDegrees(sun_Long)*2))+offset-360;
        	g2.drawLine(x,offset,x,this.getHeight());
        }
        
        
        System.out.println("====================================================");
        System.out.println();
        System.out.println("====================================================");
        
        
        if(Math.toDegrees(sun_Latt)<=90)
        {
        	int y = (int)-(Math.toDegrees(sun_Latt)*2)+offset+180;
        	g2.drawLine(offset,y,this.getWidth(),y);
        } else {
        	int y = (int)(360-(Math.toDegrees(sun_Latt)*2))+offset+180;
        	g2.drawLine(offset,y,this.getWidth(),y);
        }  
    }
    
    
    private void drawGrid(Graphics2D g2) {
              
        g2.setStroke(new BasicStroke(1.0f));
        g2.setColor(new Color(125,125,125));
        
        //draw longitude lines
        for(int x = 0; x <= this.getWidth(); x += gridSpacing*2) {
            g2.drawLine(x+offset, offset, x+offset, this.getHeight());
        }
        
        //draw latitude lines
        for(int y = 0; y <= this.getHeight(); y += gridSpacing*2) {
            g2.drawLine(offset, y+offset, this.getWidth(), y+offset);
        }
         
        
    }
    
    private void drawGridMarkers(Graphics2D g2) 
    {
        g2.setColor(new Color(0,0,0));
        
        //draw longitude lines
        for(int x = 1; x < (this.getWidth()/2)-20; x += 30*2) {
            g2.drawString(""+(x/2)+"", ((x+1)+(this.getWidth()/2)), offset-2);
            if(x != 1) {g2.drawString("-"+(x/2)+"", ((this.getWidth()/2) - (x-1)), offset-2);}
        }
        
        //draw latitude lines
        for(int y = 1; y < (this.getHeight()/2)-20; y += 30*2) {
             if(y != 1) {g2.drawString(""+(y/2)+"", 2, ((y+15)+(this.getHeight()/2)));}
            g2.drawString(""+(y/2)+"", 2, ((this.getHeight()/2) - (y-15)));
        }
    }
    
    
    private void drawHeatCells(Graphics2D g2) 
    {   
    	int num_x_cells = 360/this.gridSpacing;
    	int num_y_cells = 180/this.gridSpacing;
    	
    	int x_excess = (360-(num_x_cells*this.gridSpacing))/2;
    	int y_excess = (180-(num_y_cells*this.gridSpacing))/2;
    	
    	g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, colorOpacity));
        for(int i=0; i < this.cellGrid.size(); i++) {
        	SphericalCell cell = (SphericalCell)this.cellGrid.get(i);
        	SphericalCoordinate coord = (SphericalCoordinate)cell.getCoordinate();

            g2.setColor(getTempColor(cell.getValue()));

            int cellWidth = gridSpacing*2;
            int cellHeight = gridSpacing*2;
            
            int x_coord = (coord.getLongitude() >= num_x_cells/2)? (coord.getLongitude() - (num_x_cells/2)) : (coord.getLongitude()+(num_x_cells/2));
            int y_coord = (coord.getLatitude()-1);
            
            int x = (x_coord * cellWidth)+offset;
            int y = (y_coord * cellWidth)+offset;
           
            g2.fillRect(x, y, cellWidth, cellHeight);
            
            //Cell Debug
            //g2.setColor(new Color(0, 0, 0));
            //g2.drawString("c"+coord.getLongitude()+","+(coord.getLatitude()-1), x,y+20);
        }
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
        System.out.println("");
                
    }

    
    public Color getTempColor(double v){
        v = v/100 * 255;
        if(v > 255){
            v = 255;
        }
        if(v < 0){
            v = 0;
        }
        return new Color((int)v, 0, 0);
    }
    
    public void paintComponent(Graphics g) {
        // Center the image in this component.
        super.paintComponent(g);
        
        Graphics2D g2 = (Graphics2D) g;
        
        if(map != null) {
        	g2.drawImage(map,null,offset,offset);
        }
        
        this.drawHeatCells(g2);
        this.drawGrid(g2);
        this.drawGridMarkers(g2);
        this.drawSunPosition(g2);
    
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());

        setBackground(new java.awt.Color(255, 250, 245));
        setBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204)));
        setMaximumSize(new java.awt.Dimension(720, 360));
        setMinimumSize(new java.awt.Dimension(720, 360));
        setPreferredSize(new java.awt.Dimension(720, 360));
    }
    // </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    
}
